package dataStructure.Stack;

/**
 * @author lhn
 * @date 2024-04-20 14:43
 */

public class SingleLinkedListStackDemo {
    public static void main(String[] args) {
        SingleLinkedListStack stack = new SingleLinkedListStack(1);
        stack.push(1);
//        stack.push(2);
//        stack.push(3);
        stack.list();
        System.out.println("--------------------");
        stack.pop();
        System.out.println("--------------------");
        stack.list();
    }
}
class SingleLinkedListStack {
    private Node head = new Node(); // 头节点，不存储数据，用于简化操作
    private int maxSize;            // 栈的最大容量限制
    private int size = 0;           // 当前栈中元素的数量

    public SingleLinkedListStack(int maxSize) {
        this.maxSize = maxSize;
    }
    public boolean isFull() {
        return this.size == this.maxSize;
    }
    public boolean isEmpty() {
        return (this.size==0 && this.maxSize!=0);
    }
    public void push(int value) {
        if (isFull()) {
            System.out.println("栈满");
            return;
        }
        Node node = new Node(value);
        if (!isEmpty()) {
            node.setNext(head.getNext());
        }
        head.setNext(node);
        size ++;
    }
    public void pop() {
        if (isEmpty()) {
            System.out.println("栈空");
            return;
        }
        Node cur = head.getNext();
        Node curNext = cur.getNext();
        head.setNext(curNext);
        System.out.printf("%d\n",cur.getValue());
        size --;
    }
    public void list() {
        if (isEmpty()) {
            System.out.println("栈空");
            return;
        }
        Node temp = head.getNext();
        while (true) {
            System.out.printf("stack中的数据%d\n",temp.getValue());
            if (temp.getNext()==null){
                break;
            }
            temp = temp.getNext();
        }
    }
    public void printStack() {
        if (isEmpty()) {
            System.out.println("栈空");
            return;
        }
        Node current = head.getNext();
        while (current != null) {
            System.out.print(current.getValue() + " ");
            current = current.getNext();
        }
        System.out.println();
    }
}
class Node {
    private int value;  //数据存放位置
    private Node next;
    public Node() {
    }
    public Node(int value) {
        this.value = value;
        this.next = null;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}